<template>
  <el-card shadow="hover" class="mt-10">
    <el-button
      type="primary"
      :icon="Plus"
      :disabled="!categoryStore.category3Id"
      @click="showAddAttr"
      >添加属性</el-button
    >

    <el-table :data="attrList" border>
      <el-table-column type="index" align="center" label="序号" width="80" />
      <el-table-column prop="attrName" label="属性名" width="200" />
      <el-table-column label="属性值列表">
        <template v-slot="{ row }">
          <el-tag
            class="mr-10"
            type="success"
            v-for="attrValue in row.attrValueList"
            :key="attrValue.id"
            >{{ attrValue.valueName }}</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template v-slot="{ row }">
          <el-button type="warning" :icon="Edit" @click="updateAttr(row)" />
          <el-button type="danger" :icon="Delete" @click="deleteAttr(row.id)" />
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "AttrList",
};
</script>

<script lang="ts" setup>
import type { AttrList, AttrItem } from "@/api/product/model/attrModel";
import { getAttrListApi, deleteAttrListApi } from "@/api/product/attr";
import { Plus, Edit, Delete } from "@element-plus/icons-vue";
import { ref, watch } from "vue";
import { useCategoryStore } from "@/stores/category";
import { useAttrStore } from "@/stores/attr";
import { ElMessage, ElMessageBox } from "element-plus";

const categoryStore = useCategoryStore();
const attrList = ref<AttrList>([]);

// 封装成一个函数  后续会用
const getAttrList = async () => {
  const { category1Id, category2Id, category3Id } = categoryStore;
  attrList.value = await getAttrListApi({
    category1Id: category1Id as number,
    category2Id: category2Id as number,
    category3Id: category3Id as number,
  });
};

watch(
  () => categoryStore.category3Id,
  () => {
    const { category3Id } = categoryStore;
    if (!category3Id) {
      attrList.value = [];
      return;
    }
    getAttrList();
  },
  {
    immediate: true,
  }
);

// watch(
//   () => categoryStore.category3Id,
//   async () => {
//     const { category1Id, category2Id, category3Id } = categoryStore;
//     if (!category3Id) {
//       attrList.value = [];
//       return;
//     }
//     attrList.value = await getAttrListApi({
//       category1Id: category1Id as number,
//       category2Id: category2Id as number,
//       category3Id: category3Id as number,
//     });
//   },
//   {
//     immediate: true,
//   }
// );
const attrStore = useAttrStore();

// 点击修改
const updateAttr = (row: AttrItem) => {
  emit("setIsShowAttrList", false);
  attrStore.attrItem = row;
};

// 点击删除
const deleteAttr = (id: number) => {
  ElMessageBox.confirm("确定要删除嘛?", "警告", {
    confirmButtonText: "OK",
    cancelButtonText: "Cancel",
    type: "warning",
  })
    .then(async () => {
      await deleteAttrListApi(id);
      ElMessage.success("删除成功");
      // 删除成功重获获取列表
      getAttrList();
    })
    .catch(() => {
      ElMessage({});
    });
};

const emit = defineEmits(["setIsShowAttrList"]);

// 点击添加
const showAddAttr = () => {
  emit("setIsShowAttrList", false);
  // 重置所有数据，防止点击添加完了之后在点会留有之前的信息
  attrStore.$reset();
};
</script>

<style scoped></style>
